package com.huajin.iot.monitor.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import com.huajin.iot.monitor.domin.GatewayDO;
import com.huajin.iot.monitor.service.GatewayService;
import com.huajin.iot.common.utils.PageUtils;
import com.huajin.iot.common.utils.Query;
import com.huajin.iot.common.utils.R;

/**
 * 网关设备
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-10-10 17:17:46
 */
@Api(tags = {"网关管理接口"})
@RestController
@RequestMapping("/system/gateway")
public class GatewayController {
    @Autowired
    private GatewayService gatewayService;

    @ApiOperation("网关设备列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:gateway:gateway")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<GatewayDO> gatewayList = gatewayService.list(query);
        int total = gatewayService.count(query);
        PageUtils pageUtils = new PageUtils(gatewayList, total);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", gatewayList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }


    /**
     * 修改
     */
    @ApiOperation("更新网关设备信息")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("system:gateway:edit")
    public R update(GatewayDO gateway) {
        if (gatewayService.update(gateway) > 0) {
            return R.ok();
        } else {
            return R.error(1, "更新失败");
        }
    }

    /**
     * 删除
     */
    @ApiOperation("删除网关")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:gateway:remove")
    public R remove(Long id) {
        if (gatewayService.remove(id) > 0) {
            return R.ok();
        }
        return R.error(1, "删除失败");
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除网关")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:gateway:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        if (gatewayService.batchRemove(ids) == ids.length) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }
}
